package cur.nature.framework.core.thread;

import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.ttl.TransmittableThreadLocal;
import cur.nature.framework.core.util.JsonUtil;

/**
 *
 * @author wujian
 * 2018-05-03
 */
public class MyThreadLocal extends ConcurrentHashMap<String, Object> {
	private static final long serialVersionUID = 1L;

	protected static Class<? extends MyThreadLocal> contextClass = MyThreadLocal.class;

	private static final ThreadLocal<MyThreadLocal> threadLocal = new TransmittableThreadLocal<>(){
		@Override
		protected MyThreadLocal initialValue() {
			try {
//				return contextClass.newInstance();
				Constructor<?> constructor = contextClass.getDeclaredConstructor(); // 获取无参数构造函数
				constructor.setAccessible(true); // 如果构造函数不是public的，需要设置可访问性
				Object instance = constructor.newInstance(); // 创建实例
				return (MyThreadLocal) instance;
			} catch (Throwable e) {
				throw new RuntimeException(e);
			}
		}
	};

	public static final String THREAD_DATA = "thread_data";
	public static final String TOKEN = "token";
	public static final String REFRESH_TOKEN = "refresh_token";
	public static final String USER_ID = "user_id";
	public static final String SESSION_ID = "session_id";
	public static final String TRACE_ID = "trace_id";
	public static final String START_TIME = "start_time";
	public static final String END_TIME = "end_time";
	public static final String PARAM = "param";
	public static final String INSURANCE_SYSTEM = "insurance_system";


	public static MyThreadLocal getCurrentThread() {
		MyThreadLocal context = threadLocal.get();
		return context;
	}
//    public void set(String key,Object value){
//    	synchronized(lock){
//    		Map<String, Object> map = threadLocal.get();
//    		if(map == null){map = new HashMap<String,Object>(); threadLocal.set(map);}
//    		map.put(key, value);
//    	}
//    }

//    public Object get(String key){
//    	Map<String, Object> map = threadLocal.get();
//    	if(null == map || StringUtil.isEmpty(key)){return null;}
//    	return map.get(key);
//    }

	public void removeAll(){
		threadLocal.remove();
	}

	public static String toJson(ThreadData data){
		return JsonUtil.toJSONString(data);
	}

	public static ThreadData toData(String json){
		return JsonUtil.parseObject(json, ThreadData.class);
	}

	public static class ThreadData{

		private String token; //对userInfo进行MD5加密
		private String userInfo; //以AES对称加密成为字符串
		private Object data; //请求方自定义数据
		private String sessionId; //请求方自定义数据

		public String getToken() {
			return token;
		}
		public void setToken(String token) {
			this.token = token;
		}
		public String getUserInfo() {
			return userInfo;
		}
		public void setUserInfo(String userInfo) {
			this.userInfo = userInfo;
		}
		public Object getData() {
			return data;
		}
		public void setData(Object data) {
			this.data = data;
		}
		public String getSessionId() {
			return sessionId;
		}
		public void setSessionId(String sessionId) {
			this.sessionId = sessionId;
		}
	}

	public static class UserInfo{
		private Integer userId;
		private String userName;
		//    	private String password;
		private Integer roleId;
		public Integer getUserId() {
			return userId;
		}
		public void setUserId(Integer userId) {
			this.userId = userId;
		}
		public String getUserName() {
			return userName;
		}
		public void setUserName(String userName) {
			this.userName = userName;
		}
		public Integer getRoleId() {
			return roleId;
		}
		public void setRoleId(Integer roleId) {
			this.roleId = roleId;
		}
	}
}
